草庐IT

c++ - C++中static的含义

全部标签

c++ - "static const int"导致链接错误( undefined reference )

使用以下代码时,我对链接器错误感到困惑://static_const.cpp--completecode#includestructElem{staticconstintvalue=0;};intmain(intargc,char*argv[]){std::vectorv(1);std::vector::iteratorit;it=v.begin();returnit->value;}但是,这在链接时会失败——不知何故,它需要一个符号来表示静态const“值”。$g++static_const.cpp/tmp/ccZTyfe7.o:Infunction`main':static_con

c++ - static_cast 和 Implicit_cast 有什么区别?

什么是implicit_cast?我什么时候应该更喜欢implicit_cast而不是static_cast? 最佳答案 我正在复制我对answerthiscomment的评论在另一个地方。Youcandown-castwithstatic_cast.Notsowithimplicit_cast.static_castbasicallyallowsyoutodoanyimplicitconversion,andinadditionthereverseofanyimplicitconversion(uptosomelimits.you

c++ - 函数定义后 "const -> std::string const&"的含义?

阅读C++Primer,5thEdition中一​​个练习的答案,我发现了这段代码:#ifndefCP5_ex7_04_h#defineCP5_ex7_04_h#includeclassPerson{std::stringname;std::stringaddress;public:autoget_name()const->std::stringconst&{returnname;}autoget_addr()const->std::stringconst&{returnaddress;}};#endif是什么const->std::stringconst&在这种情况下是什么意思?

python - C++ 和 Python 之间相等含义的区别的根源是什么?

关闭。这个问题是opinion-based.它目前不接受答案。想改善这个问题吗?更新问题,以便可以通过editingthispost用事实和引文回答问题.3年前关闭。ImprovethisquestionC++和Python之间“平等”的含义似乎存在几乎哲学上的差异。我becameaware通过尝试在Python中做一些在C++中非常困难的事情来区分这种区别:区分两种枚举类型,当它们都只是一组整数的包装器时,但问题比枚举更广泛,因此是当前的问题。如果我用C++代码编写如下#includestructFoo{booloperator==(constFoo&foo)const{return

c++ - 错误 LNK2001 : unresolved external symbol "private: static class

errorLNK2001:unresolvedexternalsymbol"private:staticclassirrklang::ISoundEngine*GameEngine::Sound::_soundDevice"(?_soundDevice@Sound@GameEngine@@0PAVISoundEngine@irrklang@@A)我无法弄清楚为什么我会收到此错误。我相信我正在正确初始化。有人可以帮忙吗?声音.hclassSound{private:staticirrklang::ISoundEngine*_soundDevice;public:Sound();~Soun

c++ - C++中 "~"(波浪号)符号的含义?

//AirlineTicket.hclassAirlineTicket{public:AirlineTicket();~AirlineTicket();intgetNumberOfMiles();private:intmNumberOfMiles;};我现在想知道这段代码中的~AirlineTicket();是什么意思?我不知道~(波浪号)的含义。 最佳答案 它是析构函数。当您销毁(到达范围的末尾,或调用delete指向指向的指针)对象的实例时,它会被调用。 关于c++-C++中"~"(

c++ - 重载 static_cast 的调用不明确

我有一些这样的代码structB{B(){}B(intv){}};structA{operatorint()const{return1;}operatorB()const{returnB();}};intmain(){Aa;static_cast(a);//Errorherea.operatorB();//ThisisOKreturn0;}会产生这样的编译错误:main.cpp:Infunction‘intmain()’:main.cpp:16:21:error:callofoverloaded‘B(A&)’isambiguousstatic_cast(a);^main.cpp:4:5

c++ - 复制构造函数中省略号的含义是什么?

考虑以下程序:#includestructTest{inta;Test():a(3){}Test(constTest&t...){std::coutget_a()仔细观察复制构造函数参数中的三个点当我尝试这个程序时,我真的很惊讶。它有什么用?什么意思?语言规范对此有何评论?我知道三个点用来表示可变参数函数中的可变长度参数如printf()和scanf()等,以及C99引入的可变参数宏。在C++中,如果我没记错的话,它们是在可变参数模板中使用的。这段代码格式正确吗?这个可变参数复制构造函数可以接受任意数量的参数吗?它在g++4.8.1和MSVS2010上编译和运行良好。

c++ - static const 对命名空间成员有什么影响

//MyClass.hnamespaceMyNamespace{staticconstdoubleGasConstant=1.987;classMyClass{//constructors,methods,etc.};}我之前在MyClass声明中声明了GasConstant(并且在源文件中有单独的定义,因为C++不支持非整数类型的const初始化)。但是,我需要从其他文件访问它,而且从逻辑上讲,它似乎应该驻留在命名空间级别。我的问题是,staticconst在这种情况下有什么作用?显然const意味着我不能为GasConstant分配新值,但是命名空间中的静态成员是什么意思。这是否类

c++ - void* 与 static_cast 对比 intptr_t 与 reinterpret_cast

我想知道两种不同类型的非常特殊类型的转换表之间是否存在特定的、基于标准的差异。特别是,给定:类型T和变量T*object是:intptr_topaque=reinterpret_cast(object);T*result=reinterpret_cast(opaque);相当于:void*opaque=static_cast(object);T*result=static_cast(opaque);我只关心result,它是否保证是相同的值,相当于任何类型T的原始object?我不在乎中间opaque有什么位模式,因为我相信标准技术上允许它们在每种情况下都不同(尽管没有理智的编译器会产